//
//  NSString+Verify.m
//  GMShopProject
//
//  Created by 神廷 on 2020/10/5.
//

#import "NSString+Verify.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (Verify)


+(BOOL) isBlankString:(NSString *)string {
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (string == nil || string == NULL || [string isEqualToString:@"(null)"]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    NSRange range = [string rangeOfString:string];
    if (range.length == 0 && range.location == 0 && [string isEqual:@" "]) {
        return YES;
    }
    return NO;
}

//纯数字判断
+ (BOOL)validateNumber:(NSString *)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

//MD5加密
- (NSString *)MD5{
    const char *cStr = [self UTF8String];
    unsigned char digest[16];
    unsigned int x=(int)strlen(cStr) ;
    CC_MD5( cStr, x, digest );
    // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

//手机号码合法性判断
-(BOOL)isValidateMobile{
    NSString *MobileRegex = @"^[1][3-9][0-9]{9}$";
    NSPredicate *phoneNum = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MobileRegex];
    return [phoneNum evaluateWithObject:self];
}

//身份证合法性判断最终版;
+ (BOOL)isValidateTruePeopleIDCode:(NSString *)identityCard {
    
    BOOL flag;
    if (identityCard.length <= 0)
    {
        flag = NO;
        return flag;
    }
    
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    flag = [identityCardPredicate evaluateWithObject:identityCard];
    
    
    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if(flag)
    {
        if(identityCard.length==18)
        {
            //将前17位加权因子保存在数组里
            NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
            
            //这是除以11后，可能产生的11位余数、验证码，也保存成数组
            NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
            
            //用来保存前17位各自乖以加权因子后的总和
            
            NSInteger idCardWiSum = 0;
            for(int i = 0;i < 17;i++)
            {
                NSInteger subStrIndex = [[identityCard substringWithRange:NSMakeRange(i, 1)] integerValue];
                NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
                
                idCardWiSum+= subStrIndex * idCardWiIndex;
                
            }
            
            //计算出校验码所在数组的位置
            NSInteger idCardMod=idCardWiSum%11;
            
            //得到最后一位身份证号码
            NSString * idCardLast= [identityCard substringWithRange:NSMakeRange(17, 1)];
            
            //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
            if(idCardMod==2)
            {
                if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
                {
                    return flag;
                }else
                {
                    flag =  NO;
                    return flag;
                }
            }else
            {
                //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
                {
                    return flag;
                }
                else
                {
                    flag =  NO;
                    return flag;
                }
            }
        }
        else
        {
            flag =  NO;
            return flag;
        }
    }
    else
    {
        return flag;
    }
    
    
    
}

//判断金额字段
+ (BOOL)isNumber:(NSString *)strValue{
    if (strValue == nil || [strValue length] <= 0){
        return NO;
    }
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    NSString *filtered = [[strValue componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
    if (![strValue isEqualToString:filtered]){
        return NO;
    }
    return YES;
}

////密码合法性判断
//-(BOOL)isValidatePassword{
//    //NSString *passwordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
//    NSString *passwordRegex = @"[0-9A-Za-z]{8,32}$";
//    NSPredicate *password = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passwordRegex];
//    return [password evaluateWithObject:self];
//    
//}
//含有六位数字
-(BOOL)isValidatePhoneCode{
    NSString *codeRegex = @"^[0-9]{5}$";
    NSPredicate *phondCode = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",codeRegex];
    return [phondCode evaluateWithObject:self];
}

//十位纯数字
-(BOOL)isValidateTenPhoneCode{
    NSString *codeRegex = @"^[0-9]{10}$";
    NSPredicate *phondCode = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",codeRegex];
    return [phondCode evaluateWithObject:self];
}

//邮箱地址的正则表达式
- (BOOL)isValidateEmail{
    NSString *emailRegex = @"^[a-zA-Z0-9.!#$%&'*+/=?^_{|}~-]+@[a-zA-Z0-9]\(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9]\(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

// 匹配密码格式
+ (BOOL)isValidatePassword:(NSString *)password{
//    NSString * passwordRegex = @"[a-zA-Z0-9]{8,32}";
    NSString * passwordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,32}$";
    NSPredicate * passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
    return [passwordTest evaluateWithObject:password];
}

// 复制到剪切板
+ (void)copyToShearPlateString:(NSString *)string {
    if ([NSString isBlankString:string]) {
        return;
    }
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = string;
}

//数字和字母
- (BOOL)isValidateNumAndAz{
    
    NSString *numCheck = @"[A-Z0-9a-z]";
    
    NSPredicate *numTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",numCheck];
    
    return [numTest evaluateWithObject:self];
    
}

//获取图片宽高
+ (void)getImageSize:(NSString *)imageURL complate:(void(^)(CGSize resultSize))complate {
    __block CGSize size = CGSizeMake(kScreenWidth, kScreenWidth);
    dispatch_group_t group = dispatch_group_create();
    // 请求一
    dispatch_group_enter(group);
    NSString *urlString = [NSString getNSUTF8String:[NSString stringWithFormat:@"%@?x-oss-process=image/info",imageURL]];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
        } else {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//            NSLog(@"imageURL ==> %@",dic);
            size = CGSizeMake([dic[@"ImageWidth"][@"value"] integerValue], [dic[@"ImageHeight"][@"value"] integerValue]);
        }
        dispatch_group_leave(group);
    }];
    [dataTask resume];
//    // 请求二
//    dispatch_group_enter(group);
//    [self loadCredit:^(BOOL isSuccess) {
//        dispatch_group_leave(group);
//    }];
    // 当上述两个请求结束后，收到通知，在此做后续工作
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (complate) {
            complate(size);
        }
    });
//https://oss.milamigo.net/public/20210827/1630046077428.jpg?x-oss-process=image/info
//    if ([imageURL containsString:@"w_"] && [imageURL containsString:@"h_"]) {
//        NSArray *ary = [imageURL componentsSeparatedByString:@"w_"];
//        NSArray *ary1 = [ary[1] componentsSeparatedByString:@"h_"];
//        NSArray *ary2 = [ary1[1] componentsSeparatedByString:@"."];
//        CGSize size = CGSizeMake([ary1[0] integerValue], [ary2[0] integerValue]);
//        if (size.width <= 0 || size.height <= 0) {
//            return CGSizeMake(kScreenWidth, kScreenWidth);
//        }
//        return size;
//    }
//    return CGSizeMake(kScreenWidth, kScreenWidth);
}

//认证使用
- (NSString *)Base64EncodedStringFromString:(NSString *)string {
    NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
    NSUInteger length = [data length];
    NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    
    uint8_t *input = (uint8_t *)[data bytes];
    uint8_t *output = (uint8_t *)[mutableData mutableBytes];
    
    for (NSUInteger i = 0; i < length; i += 3) {
        NSUInteger value = 0;
        for (NSUInteger j = i; j < (i + 3); j++) {
            value <<= 8;
            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }
        
        static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        
        NSUInteger idx = (i / 3) * 4;
        output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
        output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
        output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6)  & 0x3F] : '=';
        output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0)  & 0x3F] : '=';
    }
    
    return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
}

//是否有中文
- (BOOL)isValidateChinese{
    NSString *chineseRegex = @"^[\u4e00-\u9fa5]*$";
    NSPredicate *chinese = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",chineseRegex];
    return [chinese evaluateWithObject:self];
}
//网址判断
- (BOOL)isValidateUrl
{
    NSString *regex =@"[a-zA-z]+://[^\\s]*";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [urlTest evaluateWithObject:self];
}
+(BOOL)isValidateUrlString:(NSString*)urlString {
    if (!urlString) {
        return NO;
    }
    NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+\\.(.)+";
    NSPredicate *urlPredic = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    return [urlPredic evaluateWithObject:urlString];
}
//字符串内容是否是有效数字
- (BOOL)isValidateNumberByRegExp {
    BOOL isValid = YES;
    NSUInteger len = self.length;
    if (len > 0) {
        NSString *numberRegex = @"^[0-9]*$";
        NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
        isValid = [numberPredicate evaluateWithObject:self];
    }
    return isValid;
}
//提取s字符串手机号
+ (NSString *)extractMobileWithString:(NSString *)string parameter:(NSString *)str
{
    NSArray *arr = [string componentsSeparatedByString:str];
    if ([arr[1] containsString:@"&"]) {
        NSArray *arr1 = [arr[1] componentsSeparatedByString:@"&"];
        return arr1[0];
    }else{
        return arr[1];
    }
}
+ (BOOL)isValidateBankCard:(NSString *)cardNumber {
    if(cardNumber.length==0)
    {
        return NO;
    }
    NSString *digitsOnly = @"";
    char c;
    for (int i = 0; i < cardNumber.length; i++)
    {
        c = [cardNumber characterAtIndex:i];
        if (isdigit(c))
        {
            digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
        }
    }
    int sum = 0;
    int digit = 0;
    int addend = 0;
    BOOL timesTwo = false;
    for (NSInteger i = digitsOnly.length - 1; i >= 0; i--)
    {
        digit = [digitsOnly characterAtIndex:i] - '0';
        if (timesTwo)
        {
            addend = digit * 2;
            if (addend > 9) {
                addend -= 9;
            }
        }
        else {
            addend = digit;
        }
        sum += addend;
        timesTwo = !timesTwo;
    }
    int modulus = sum % 10;
    return modulus == 0;
}


#pragma mark - 把时间戳格式化为 YYYY-MM-dd HH:mm:ss的格式
+ (NSString *)dateFomatterStringWithYMDHMS:(NSString *)time {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // （@"YYYY-MM-dd HH:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[time integerValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

+ (NSString *)convertToJsonData:(NSDictionary *)dict{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}


+ (NSString *)handleJumpPagesWithString:(NSString *)string {
    NSString *handleString = string.lowercaseString;
    if (![handleString hasPrefix:@"dk://"]) {
        return string;
    };
    NSString *tempString = [handleString substringFromIndex:5];
    return tempString;
}

+ (NSString *)getHiddenPhoneNumberUseString:(NSString *)originPhoneNumber{
    //校验长度，防止传入数据不对引起崩溃
    if (originPhoneNumber.length >= 7) {
        return [originPhoneNumber stringByReplacingCharactersInRange:NSMakeRange(3,4) withString:@"****"];
    } else {
        return originPhoneNumber;
    }
}

+ (NSString *)getHiddenIDCardNumberUseString:(NSString *)originPhoneNumber{
    //校验长度，防止传入数据不对引起崩溃
    if (originPhoneNumber.length >= 18) {
        return [originPhoneNumber stringByReplacingCharactersInRange:NSMakeRange(4,10) withString:@"**********"];
    } else {
        return originPhoneNumber;
    }
}

@end
